$(document).ready(function(){  
    
    // Set children ages visible and clears inputs
    do_childs2 = function(nr){

        nr = parseInt(nr);

        if(nr==0){
            $(".ages2").hide();
        }else{
            $(".ages2").show();

            for(i=1;i<=nr;i++){
                $(".age-1"+i).show();
            }
            for(i=nr+1;i<=5;i++){
                $(".age-1"+i+" input").val("");
                $(".age-1"+i).hide();
            }
        }
    }

    // Add event
    $("#nr_childrens2").live("change",function(){
        do_childs2($(this).val());
    })

    // Do form validation
    $('.confirm_availability').live("click",function(){ 

        // do validation for childs age
        nr = $("#nr_childrens2").val();

        for(i=1;i<=nr;i++){

            if($(".age-1"+i+" input").val()==""){

                $("#freeow").freeow("", "Please fill age(s) of children.", {
                    classes: ["error"],  
                    autoHide: true,
                    autoHideDelay: 5000,
                    hideDuration: 1500
                });

                return false;
            }
        }
    })

    //Do Reset Childs Ages
    if(doResetAges){

        // do resete childs and ages
        $("#nr_childrens2").val(0);

        for(i=1;i<=5;i++){
            $(".age-1"+i+" input").val("");
        } 
     
    }
     
    // set visible childs ages based on value of nr_childern
    do_childs2(nr_children);
     
     
    /***********************************************************
        * JQUERY UI Calendar with event
        ***********************************************************/
    init_dates = function () {

        //console.log('init');

        var date = new Date();
        var start = "#ckeck_in2";
        var end = "#check_out2";

        //  limiter 1 - today is not available
        //  limiter 0 - today is available
        var limiter = 1;

        var m = date.getMonth(),
        d = date.getDate() + limiter,
        y = date.getFullYear();

        // Disable all dates till today
        $(start).datepicker({
            minDate:new Date(y, m, d),
            dateFormat:'dd.mm.yy',
            showOn:'both',
            buttonImage:base_url + "assets/img/date-bgr.png",
            buttonImageOnly:true,
            nextText:'&raquo;',
            prevText:'&laquo;',
            onSelect:function (dateStr) {

                var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);

                $(end).datepicker("option", "minDate", depart);

                depart.setDate(depart.getDate() + limiter);
                $(end).datepicker("setDate", depart);

            }
        });

        $(end).datepicker({
            minDate:new Date(y, m, d),
            dateFormat:'dd.mm.yy',
            showOn:'both',
            buttonImage:base_url + "assets/img/date-bgr.png",
            buttonImageOnly:true,
            nextText:'&raquo;',
            prevText:'&laquo;',
            onSelect:function (dateStr) {
            //$(hidden_end).val($.datepicker.formatDate('dd.mm.yy', $(end).datepicker('getDate')));
            }
        })

        // SET DATE
        $(start).datepicker('setDate', new Date(y, m, d));
        $(end).datepicker('setDate', new Date(y, m, d + 1));

        // Set custom class
        $("#ui-datepicker-div").addClass("newcalendar");
        $("#ui-datepicker-div").addClass("singleCalendar");


        function set_checkin_checkout(){
            
            //set pickupdate from session
            var n1 = check_in.split(".");
            $(start).datepicker('setDate', new Date(n1[2], n1[1] - 1, n1[0]));

            // set mindate for checkout
            $(end).datepicker("option", "minDate", new Date(n1[2], n1[1] - 1, parseInt(n1[0]) +1));

            //set returndate from session
            var n2 = check_out.split(".");
            $(end).datepicker('setDate', new Date(n2[2], n2[1] - 1, n2[0]));
        }

        // METHOD IS SEARCH - set dates from $session variable
        if(method=="search" || method == "details"){

            set_checkin_checkout();

        }
          
        if(typeof is_post_room_search === 'undefined'){
               
        }else{
               
            set_checkin_checkout();
               
        }

    }
     
    init_dates();


    // Hotel reviews
    var reviews_active = false;
    $(".view_all_reviews").live('click',function(){
    
        if(reviews_active){
        
            reviews_active = false;
            $(this).text('See all reviews');
            $('#hotel-reviews').hide();
            $('#swich_hotel_content').show();
        
        }else{
        
            reviews_active = true;
            $(this).text('Back to hotel');
            $('#hotel-reviews').show();
            $('#swich_hotel_content').hide();
        
        }
    
    })
    
    $('#f_name').val("");
    $('#l_name').val("");
    $('#score').val('9.0');
    $('#comments').val("");
    
    var formbutton = '#sub_reviews';
    var submit_disabled = false;
    
    
    $(formbutton).live('click',function(){
        if(!submit_disabled)
            $('#review_form').submit();
    })
    
    //FORM VALIDATION
    $("#review_form").validate({
        rules: {
            f_name: 'required', 
            l_name: 'required', 
            comments: 'required'
               
        }, 
        highlight: function(element) {
            $(element).addClass("error");
        },
        unhighlight: function(element) {
            $(element).removeClass("error");
        },
        submitHandler: function() {
            
            submit_disabled = true;
            
            $(formbutton).busy({
                img:base_url+'assets/img/backgnds/busy.gif',
                position : 'left'
            });

            var my_form = $("#review_form");
            var form_data = my_form.serialize();
            var url = my_form.attr('action');
            
            $.ajax({
                url: url,
                data:form_data,
                dataType: 'json',
                success: function(data){
                    
                    $(formbutton).busy('hide');
                    
                    if(data.success){
                        alert('Thank you for your comment.');
                        window.location.reload();
                    }else{
                        alert('Please try againg.');
                        submit_disabled = false;
                    }
                    
                    
                
                }
            });
               
            return false;
        },
        errorPlacement: function(error, element){}
    })
    
    // Add open / close rooms details
    $('.roomHandler').live('click',function(){
        
        // chang icon arrow
        // add hidden class
        // show hidden row
        // and viceversa
        if($(this).closest('tr').find('.tooltip a.roomHandler').hasClass('toggle_hide')){
            $(this).closest('tr').find('.tooltip a.roomHandler').removeClass('toggle_hide');
            $(this).closest('tr').next().hide();
            $(this).closest('tr').next().find('td').css('border-top','1px solid #F2F2F2');
            $(this).closest('tr').find('td').css('border-bottom','1px solid #F2F2F2');
        }else{
            $(this).closest('tr').find('.tooltip a.roomHandler').addClass('toggle_hide');
            $(this).closest('tr').next().show();
            $(this).closest('tr').next().find('td').css('border-top','none');
            $(this).closest('tr').find('td').css('border-bottom','none');    
            
        }
        
    })
    
    $('.close_button_roomstable').live('click',function(){
        $(this).closest('tr').prev().find('.tooltip a.roomHandler').trigger('click');
    })
    
});